package com.feicheng.controller.wx;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.common.task.Cache;
import com.feicheng.common.util.StringGenerator;
import com.feicheng.persistence.entity.*;
import com.feicheng.persistence.service.*;
import com.feicheng.service.a.FDzService;
import com.feicheng.wxapi.app.WxApi;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("/wx/dz")
@RequestMapping("/wx/dz")
public class DzController {

    @Autowired
    FDzService dzService;

    @Autowired
    IAmountService iAmountService;

    @Autowired
    IClientService clientService;

    @Autowired
    IAdzService adzService;

    @Autowired
    IAzjService azjService;

    @Autowired
    IAzjgsService azjgsService;

    @Autowired
    IAcommissionService acommissionService;

    @Autowired
    IAmdjlService amdjlService;

    @Autowired
    IAmdjlsqitemService amdjlsqitemService;

    @Autowired
    WxApi wxApi;

    @Autowired
    Cache cache;

    @Autowired
    HttpSession session;

    @PostMapping("/get")
    public ResultInfo newMd(@RequestBody JSONObject param) {
        param = dzService.getDz(param);
        return ResultInfo.success(param);
    }

    @Transactional
    @GetMapping("/login/jscode")
    public ResultInfo jscode(String code, String fromid, String phoneCode) {
        JSONObject result = new JSONObject();

        Asetting asetting = cache.getObject("Asetting", Asetting.class);

        String openId = wxApi.getOpenId(
                code,
                asetting.getDappid(),
                asetting.getDsecretkey());

        /* 取得电话号 */
//        String phoneCode = param.getString(Constant.WX.PARAM.PHONE_CODE);
//        String phone = wxApi.getPhoneNumber(
//                openId,
//                appInfo.getAppId(),
//                appInfo.getAppSecret(),
//                phoneCode);

        QueryWrapper<Client> clientQueryWrapper = new QueryWrapper<Client>();
        clientQueryWrapper.eq("openid", openId);
        List<Client> clients = clientService.list(clientQueryWrapper);
        if (0 == clients.size()) {
            Client client = new Client();
            client.setOpenid(openId);
            client.setAppid(asetting.getDappid());
            client.setClientid(StringGenerator.getID("CLI"));
            client.setFromid(fromid);
            client.setHeadimg("https://wimg.588ku.com/gif620/21/04/25/26c111bdfb2b4c9e872d83f530a86e4b.gif");
            clientService.save(client);

            Amount amount = new Amount();
            amount.setAmount(0);
            amount.setOpenid(openId);
            iAmountService.save(amount);

            result.put("client", client);
            result.put("amount", amount);

            Adz adz = new Adz();
            adz.setDzid(StringGenerator.getID("DZ"));
            adz.setOpenid(openId);
            adz.setCover("https://wimg.588ku.com/gif620/20/09/11/1e6354a487be8ff16f8495542c14f980.gif");
            adz.setClientid(client.getClientid());
            adzService.save(adz);
            result.put("adz", adz);

            Amdjl amdjl = new Amdjl();
            amdjl.setMdjlid(StringGenerator.getID("MDJL"));
            amdjl.setClientid(client.getClientid());
            amdjl.setOpenid(openId);
            amdjlService.save(amdjl);
            result.put("amdjl", amdjl);

            Azj azj = new Azj();
            azj.setZjid(StringGenerator.getID("ZJ"));
            azj.setClientid(client.getClientid());
            azj.setOpenid(openId);
            azj.setCommissionid(StringGenerator.getID("COMM"));

            Acommission acommission = new Acommission();
            acommission.setCommissionid(azj.getCommissionid());
            acommission.setTotal(0);
            acommissionService.save(acommission);
            azjService.save(azj);
            result.put("azj", azj);

        } else if (1 == clients.size()) {
            Client client = clients.remove(0);
            result.put("client", client);
            result.put("amount", iAmountService.getById(client.getOpenid()));

            QueryWrapper<Adz> dqw = new QueryWrapper<>();
            dqw.eq("clientid", client.getClientid());
            dqw.eq("openid", client.getOpenid());
            Adz adz = adzService.getOne(dqw);
            result.put("adz", adz);

            QueryWrapper<Amdjl> mdjlqw = new QueryWrapper<>();
            mdjlqw.eq("clientid", client.getClientid());
            mdjlqw.eq("openid", client.getOpenid());
            Amdjl amdjl = amdjlService.getOne(mdjlqw);
            result.put("amdjl", amdjl);

            QueryWrapper<Amdjlsqitem> sqqw = new QueryWrapper<>();
            sqqw.eq("mdjlid", amdjl.getMdjlid());
            result.put("sqs", amdjlsqitemService.list(sqqw));

            QueryWrapper<Azj> zqw = new QueryWrapper<>();
            zqw.eq("clientid", client.getClientid());
            zqw.eq("openid", client.getOpenid());
            Azj azj = azjService.getOne(zqw);
            result.put("azj", azj);

            if (StringUtils.hasLength(azj.getZjgsid())) {
                Azjgs azjgs = azjgsService.getById(azj.getZjgsid());
                result.put("azjgs", azjgs);
            }

        } else {
            throw new SystemException("当前账户非法");
        }

        session.setAttribute(Constant.RESULT.CURRENT_CLIENT, result);

        return ResultInfo.success(result);
    }

    @Transactional
    @PostMapping("/phone")
    public ResultInfo changePhone(@RequestBody JSONObject param) {

        String clientid = param.getString("clientid");
        String dzid = param.getString("dzid");
        String phoneCode = param.getString("phoneCode");
        JSONObject result = new JSONObject();

        Asetting asetting = cache.getObject("Asetting", Asetting.class);

        String phone = wxApi.getPhoneNumber(
                "",
                asetting.getDappid(),
                asetting.getDsecretkey(),
                phoneCode);


        Adz adz = adzService.getById(dzid);
        Client client = clientService.getById(adz.getClientid());

        client.setTel(phone);
        adz.setTel(phone);

        clientService.updateById(client);
        adzService.updateById(adz);

        result.put("client", client);
        result.put("adz", adz);

        Amount amount = iAmountService.getById(client.getOpenid());
        result.put("amount", amount);

        session.setAttribute(Constant.RESULT.CURRENT_CLIENT, result);

        return ResultInfo.success(result);
    }

    @PostMapping("/info/put")
    public ResultInfo updateAdz(@RequestBody JSONObject param) {
        Adz adz = param.getObject("adz", Adz.class);
        if(!StringUtils.hasLength(adz.getDzid())){
            throw new SystemException("店长id不能为空");
        }
        adzService.updateById(adz);
        param.put("adz", adz);
        return ResultInfo.success(param);
    }

}
